const fs = require('fs')
const path = require('path')
const puppeteer = require('puppeteer-core');

//获取顶部导航条数据
function getTopBar(page) {
  return page.evaluate(() => {
    let navTextArr = []
    $('.topbar-nav a').each(function (index, item) {
      let obj = {
        text: $(item).text(),
        href: $(item).attr('href')
      }
      navTextArr.push(obj)
    })
    return navTextArr
  })
}

//获取头部导航条数据
function getHeaderNav(page) {
  return page.evaluate(() => {
    let navItem = $('.header-nav li.nav-item')
    let arr = []

    navItem.each(function (index, item) {
      let navMenu = []
      $(item).find('.item-children .children-list > li').each(function (i, item) {
        navMenu.push({
          imgUrl: $(item).find('.figure img').attr('data-src'),
          href: $(item).find('a').attr('href'),
          title: $(item).find('.title').text(),
          price: $(item).find('.price').text(),
          flags: $(item).find('.flags .flag').text() || ''
        })
      })
      arr.push({
        href: $(item).find('a.link').attr('href'),
        text: $(item).find('a.link').text(),
        navMenu: navMenu
      })
    })
    return arr
  })
}

//获取轮播图
function getCarousel(page) {
  return page.evaluate(() => {
    let slides = $('#J_homeSlider .slide')
    let arr = []
    slides.each((i, item) => {
      let str = $(item).attr('data-bg-set')
      if (str) {
        let imgUrl = JSON.parse(str.replace(/'/g, '"'))
        arr.push(imgUrl.img)
      } else {
        arr.push($(item).find('img').attr('src'))
      }
    })
    return arr
  })
}

//左侧边栏分类
function getCategory(page) {
  return page.evaluate(() => {
    let list = $('#J_categoryList .category-item')
    let cateList = []
    list.each((i, item) => {
      let title = $(item).find('.title').get(0).firstChild.textContent
      let arr = []
      $(item).find('.children > ul > li').each((i, item) => {
        arr.push({
          text: $(item).find('.text').text(),
          imgUrl: $(item).find('img').attr('data-src'),
        })
      })
      cateList.push({
        title: title,
        list: arr
      })
    })
    return cateList
  })
}

function getFlashPurchase(page) {
  return page.evaluate(() => {
    let goodsList = $('.xm-flashPurchase .xm-flashPurchase-list .item')
    let arr = []
    goodsList.each((i, item) => {
      let imgUrl = $(item).find('.content img').attr('src')
      let title = $(item).find('.content .title').text()
      let desc = $(item).find('.content .desc').text()
      let newPrice = $(item).find('.content .price span:first-child').text()
      let oldPrice = $(item).find('.content .price del').text()
      arr.push({
        imgUrl, title, desc, newPrice, oldPrice
      })
    })
    return arr
  })
}

//获取所有的banner相关数据
async function getBannerBox(page) {
  return page.evaluate(() => {
    let bannerBoxs = $('.home-main .home-banner-box img')
    // let brickBoxs = $('.home-main .home-brick-box')
    //普通商品列表展示模块
    let brickBoxs = $('.home-main .J_brickBox')
    //推荐模块

    let arr = []

    brickBoxs.each((i, item) => {
      let brickItems = $(item).find('.J_brickBd .brick-promo-list .brick-item')
      let brickLists = $(item).find('.J_brickBd .brick-list')
      let more = $(item).find('.box-hd .more .more-link').contents().filter(function () {
        return this.nodeType == 3
      }).text() || $.makeArray($(item).find('.box-hd .more .tab-list li'))
      let title = $(item).find('.box-hd:first-child .title').text()
      if ($.isArray(more)) {
        let moreArr = []
        $(more).each((i, item) => {
          moreArr.push($(item).text())
        })
        more = moreArr
      }
      let brickItemArr = []
      let brickListArr = []
      //遍历商品列表左边图片
      brickItems.each((i, item) => {
        brickItemArr.push($(item).find('img').attr('src'))
      })
      // brickItemArr.push(brickItems)
      //遍历商品列表
      brickLists.each((i, item) => {
        let itemArr = []
        $(item).find('.brick-item').each((i, item) => {
          if ($(item).find('.figure-img').length > 0) {
            itemArr.push({
              figure: $(item).find('.figure-img img').attr('src'),
              title: $(item).find('.title').text(),
              desc: $(item).find('.desc').text(),
              price: {
                now: $(item).find('.price > .num:first-of-type').text(),
                old: $(item).find('.price del').text() || ''
              },
              flag: $(item).find('.flag ').text() ? {
                text: $(item).find('.flag ').text(),
                style: String($(item).find('.flag').attr('class')).replace('flag', '') || ''
              } : '',
              review: {
                text: $(item).find('.review-wrapper .review').text(),
                author: $(item).find('.review-wrapper .author').text()
              }
            })
          } else if ($(item).find('.figure-more').length > 0) {
            itemArr.push({
              text: $(item).find('.more').contents().filter(function () {
                return this.nodeType == 3
              }).text(),
              sub: $(item).find('.more small').text()
            })
          }

        })
        brickListArr.push({
          itemArr
        })
      })

      arr.push({
        id: $(item).attr('id'),
        imgUrl: $(bannerBoxs[i]).attr('data-original') || $(bannerBoxs[i]).attr('src'),
        title: title,
        more: more,
        brickItemArr,
        brickListArr,
      })
    })

    return arr
  })
}

//获取推荐商品列表
async function getRecommendList(page) {
  //等待目标选择器出现在进行操作
  await page.waitForSelector('#recommend #recommend-bd .J_xm-recommend-list', {timeout: 90000})
  return page.evaluate(() => {
    let recommend = $('#recommend #recommend-bd .J_xm-recommend-list')
    //遍历推荐商品列表
    let recommendList = []
    recommend.each((i, item) => {
      recommendList.push({
        imgUrl: $(item).find('img').attr('src'),
        name: $(item).find('.xm-recommend-name').text(),
        price: $(item).find('.xm-recommend-price').text(),
        tips: $(item).find('.xm-recommend-tips').text(),
        notice: $(item).find('.xm-recommend-notice').text()
      })
    })
    return recommendList
  })
}

//获取内容列表
function getContentList(page) {
  return page.evaluate(() => {
    //内容模块
    let content = $('#content .content-list .content-item')
    let contentList = []
    content.each((i, item) => {
      let title = $(item).find('.title').text()
      let carouselArr = []
      $(item).find('.xm-carousel-wrapper .item-list li').each((i, item) => {
        if ($(item).hasClass('more')) {
          carouselArr.push({
            desc: $(item).find('.desc').text(),
            linkText: $(item).find('a').text(),
            figure: $(item).find('.thumb').attr('src'),
          })
        } else {
          carouselArr.push({
            name: $(item).find('.name').text(),
            desc: $(item).find('.desc').text(),
            price: $(item).find('.price').text(),
            figure: $(item).find('.figure img').attr('src'),
          })
        }
      })
      contentList.push({
        title,
        carouselArr
      })
    })
    return contentList
  })
}
//获取底部友情链接
function getFooterLinks(page){
  return page.evaluate(()=>{
    let links = $('.site-footer .footer-links .col-links')
    let arr = []
    links.each((i,item)=>{
      let obj = {}
      let dds = []
      $(item).find('dd').each((i,item)=>{
        dds.push($(item).text())
      })
      obj.dt = $(item).find('dt').text()
      obj.dds =dds
      arr.push(obj)
    })
    return arr
  })
}

//启用puppeteer爬虫程序
(async () => {
  let width = 1366
  let height = 900
  const browser = await puppeteer.launch({
    executablePath: 'D:\\otherApp\\chrome-win\\chrome.exe', headless: false, slowMo: 100,
    ignoreHTTPSErrors: true,
    args: [
      `--window-size=${width},${height}`,
      '--no-sandbox',
      '--disable-setuid-sandbox',
      '–disable-gpu',
      '–disable-dev-shm-usage',
      '–disable-setuid-sandbox',
      '–no-first-run',
      '–no-sandbox',
      '–no-zygote',
      '–single-process'
    ], timeout: 0
  });
  const page = await browser.newPage();
  await page.setDefaultNavigationTimeout(0);
  await page.goto('https://www.mi.com/', {waitUntil: 'networkidle0'});
  await page.setViewport({width: 1566, height: 860});
  await page.addScriptTag({url: 'https://code.jquery.com/jquery-3.2.1.min.js'})
  // await page.keyboard.sendCharacter("nba"); //模拟输入
  // await page.click("#searchBtn")//模拟点击
  await page.waitFor(1000);    //监听页面 load 完成
  await autoScroll(page);
  let arr = {
    status: 200,
    code: 0,
    data: {
      topBar: await getTopBar(page), //获取顶部导航数据
      headerBar: await getHeaderNav(page),//获取头部导航数据
      category: await getCategory(page),//获取左侧边栏分类
      carouselImg: await getCarousel(page),//获取轮播图
      flashPurchase: await getFlashPurchase(page),//获取闪购商品列表
      bannerItems: await getBannerBox(page),//获取闪购商品列表
      recommendList: await getRecommendList(page),//获取闪购商品列表
      contentList: await getContentList(page),//获取闪购商品列表
      footerLinks: await getFooterLinks(page),//获取底部友情链接
    }
  }
  console.dir(arr.toString())
  fs.writeFile(path.join(__dirname, 'home.json'), JSON.stringify(arr), function (err) {
    if (err) {
      console.log('文件写入失败！')
    } else {
    }
    console.log('文件写入成功！')
  })
  await page.screenshot({path: 'mi.png'});
  await browser.close();

})()

//自动滚动页面
async function autoScroll(page) {
  await page.evaluate(async () => {
    await new Promise((resolve, reject) => {
      let totalHeight = 0;
      let distance = 100;
      let timer = setInterval(() => {
        let scrollHeight = document.body.scrollHeight;
        window.scrollBy(0, distance);
        totalHeight += distance;

        if (totalHeight >= scrollHeight) {
          clearInterval(timer);
          resolve();
        }
      }, 100);
    });
  });
}
